package cn.wjh.business.controller;

import cn.wjh.business.domain.Views;
import cn.wjh.business.service.IViewsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 浏览Controller
 *
 * @author jiraWu
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/business/views")
public class ViewsController extends BaseController {
	@Resource
	private IViewsService viewsService;

	/**
	 * 查询浏览列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:views:list')")
	@GetMapping("/list")
	public TableDataInfo list(Views views) {
		startPage();
		List<Views> list = viewsService.selectViewsList(views);
		return getDataTable(list);
	}

	@GetMapping("/my")
	public TableDataInfo myList(Views views) {
		startPage();
		views.setUserId(SecurityUtils.getUserId());
		List<Views> list = viewsService.selectViewsList(views);
		return getDataTable(list);
	}

	/**
	 * 导出浏览列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:views:export')")
	@Log(title = "浏览", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Views views) {
		List<Views> list = viewsService.selectViewsList(views);
		ExcelUtil<Views> util = new ExcelUtil<Views>(Views.class);
		util.exportExcel(response, list, "浏览数据");
	}

	/**
	 * 获取浏览详细信息
	 */
//	@PreAuthorize("@ss.hasPermi('business:views:query')")
	@GetMapping(value = "/{viewsId}")
	public AjaxResult getInfo(@PathVariable("viewsId") Long viewsId) {
		return success(viewsService.selectViewsByViewsId(viewsId));
	}

	/**
	 * 新增浏览
	 */
//	@PreAuthorize("@ss.hasPermi('business:views:add')")
	@Log(title = "浏览", businessType = BusinessType.INSERT)
	@PostMapping
	@Anonymous
	public AjaxResult add(@RequestBody Views views) {
		return toAjax(viewsService.insertViews(views));
	}

	/**
	 * 修改浏览
	 */
//	@PreAuthorize("@ss.hasPermi('business:views:edit')")
	@Log(title = "浏览", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Views views) {
		return toAjax(viewsService.updateViews(views));
	}

	/**
	 * 删除浏览
	 */
//	@PreAuthorize("@ss.hasPermi('business:views:remove')")
	@Log(title = "浏览", businessType = BusinessType.DELETE)
	@DeleteMapping("/{viewsIds}")
	public AjaxResult remove(@PathVariable Long[] viewsIds) {
		return toAjax(viewsService.deleteViewsByViewsIds(viewsIds));
	}
}
